package com.lovo;

import java.io.*;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Properties;

public class Test02 {
    /**
     * 对象序列化
     */
    public void testObjectOutput(){
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        Student student = new Student("小强",20,"男");
        try {
            fos = new FileOutputStream("d:/edu/1.dat");
            oos = new ObjectOutputStream(fos);
            oos.writeObject(student);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void testObjectInput(){
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        try {
            fis = new FileInputStream("d:/edu/1.dat");
            ois = new ObjectInputStream(fis);
            Student stu = (Student)ois.readObject();
            System.out.println(stu.getName() + " " + stu.getAge() + " " + stu.getGender());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(fis != null ){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
//        Test02 test02 = new Test02();
//        test02.testObjectOutput();
//        test02.testObjectInput();
//        Student stu = new Student("小强",25,"男");
//        System.out.println(stu);
//        FileUtil.writeObject("student.dat",stu);

        Student stu1 = (Student)FileUtil.readObject("student.dat");
        System.out.println(stu1.getName() + stu1.getAge());

//        ArrayList list = new ArrayList();
//        list.add(100);
//        list.add("hello");
//        list.add(new Student("小强",25,"男"));

//        FileUtil.writeObject("list.dat",list);

//        ArrayList list = (ArrayList) FileUtil.readObject("list.dat");
//        System.out.println(list);
        // 请问Java中产生对象的方法有几种
        // 1. new
        // 2. 反序列化
        // 3. clone
        // 4. 反射

    }
}
